<template>
	<view>
		<u-loading-page loading-text="加载中..." :loading="showLoading" bg-color="#e8e8e8"></u-loading-page>
		<view class="loginBtn" v-if="!showLoading">
			<u-button type="primary" open-type="getUserInfo" lang="zh_CN" @click="doLogin()">重新授权登录</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showLoading: true,
				codeData: "",
				switchPath: ["index", 'community', 'discuss', 'growup', 'my'],
				option: {}
			}
		},
		onShow() {
			//uni.clearStorageSync();
		},
		onLoad(option) {
			//uni.clearStorageSync();
			this.option = option;
			console.log(option)
			uni.getStorage({
				key: 'userInfo',
				success: (res) => {
					if (res.data && (res.data.role || res.data.role === 0)) {
						uni.setStorage({
							key: "authInfo",
							data: res.data.role
						})
						if (option.path) {
							if (this.switchPath.indexOf(option.path) > -1) {
								uni.switchTab({
									url: "/pages/" + option.path + '/' + option.path
								})
							} else {
								uni.redirectTo({
									url: "/pages/" + option.path + '/' + option.path + '?' + this
										.paramsUrl(option)
								})
							}
							//分享加积分
							this.$request.get(this.$api.shareForward, {
								forwarderId: option.userId,
								postsId: option.path === 'postDetail' ? option.id : ''
							}).then(res => {
								console.log(1, res)
								uni.hideLoading()
							})
						} else {
							uni.switchTab({
								url: "../index/index"
							})
						}
					} else {
						uni.getStorage({
							key: 'authInfo',
							success: (res) => {
								if (option.path) {
									if (this.switchPath.indexOf(option.path) > -1) {
										uni.switchTab({
											url: "/pages/" + option.path + '/' + option
												.path
										})
									} else {
										uni.redirectTo({
											url: "/pages/" + option.path + '/' + option
												.path + '?' + this.paramsUrl(option)
										})
									}
									//分享加积分
									this.$request.get(this.$api.shareForward, {
										forwarderId: option.userId,
										postsId: option.path === 'postDetail' ? option.id : ''
									}).then(res => {
										console.log(1, res)
										uni.hideLoading()
									})
								} else {
									uni.switchTab({
										url: "../index/index"
									})
								}
							},
							fail(err) {
								uni.redirectTo({
									url: "../auth/auth?" + this.paramsUrl(option)
								})
							}
						});
					}
				},
				fail: () => {
					this.doInit();
				}
			});
		},
		methods: {
			getInfo(res) {
				this.$request.get(this.$api.getOpenId, {
					code: this.codeData.code
				}).then(result => {
					uni.hideLoading()
					let loginData = JSON.parse(result.result)
					this.$request.post(this.$api.getUserInfo, {
						"encrypted": res
							.encryptedData,
						"iv": res
							.iv,
						"openId": loginData
							.openid,
						"sessionKey": loginData
							.session_key,
						forwarderId: (this.option ? this.option.userId : ""),
					}).then(info => {
						uni.hideLoading()
						if (info.result) {
							uni.setStorage({
								key: "userInfo",
								data: info.result
							})
							let option = this.option;
							if (info.result.role || info.result.role === 0) {
								uni.setStorage({
									key: "authInfo",
									data: info.result.role
								})
								if (option.path) {
									//分享加积分
									this.$request.get(this.$api.shareForward, {
										forwarderId: option.userId,
										postsId: option.path === 'postDetail' ? option.id : ''
									}).then(res => {
										console.log(1, res)
										uni.hideLoading()
									})
									if (this.switchPath.indexOf(option.path) > -1) {
										uni.switchTab({
											url: "/pages/" + option.path + '/' + option.path
										})
									} else {
										uni.redirectTo({
											url: "/pages/" + option.path + '/' + option.path +
												'?' + this.paramsUrl(option)
										})
									}
								} else {
									uni.switchTab({
										url: "../index/index"
									})
								}
							} else {
								uni.redirectTo({
									url: "../auth/auth?" + this.paramsUrl(option)
								})
							}
						} else {
							uni.showToast({
								icon: "error",
								title: "服务异常，无法获取用户信息"
							})
						}
					})
				})
			},
			doInit() {
				uni.login({
					success: (codeData) => {
						console.log("wx.login：" + codeData.code)
						this.codeData = codeData;
						uni.getSetting({
							success: (res) => {
								console.log("授权：", res);
								if (!res.authSetting['scope.userInfo']) {
									//这里调用授权
									console.log("当前未授权");
									this.showLoading = false;
								} else {
									//用户已经授权过了
									console.log("当前已授权");
									uni.showModal({
										title: '温馨提示',
										content: '请授权',
										success: (res) => {
											if (res.confirm) {
												uni.getUserProfile({
													desc: '获取你的昵称、头像、地区及性别',
													success: res => {
														console.log(res)
														this.getInfo(res)
													},
													fail: res => {
														//拒绝授权
														uni.showToast({
															title: '您拒绝了请求,不能正常使用小程序',
															icon: 'none',
															duration: 2000
														});
														this.showLoading =
															false;
														return;
													}
												});
											} else if (res.cancel) {
												//如果用户点击了取消按钮
												uni.showToast({
													title: '您拒绝了请求,不能正常使用小程序',
													icon: 'none',
													duration: 2000
												});
												this.showLoading = false;
												return;
											}

										}
									})
								}
							}
						})
					},
				})
			},
			doLogin() {
				uni.getUserProfile({
					desc: '获取你的昵称、头像、地区及性别',
					success: res => {
						this.getInfo(res)
					},
					fail: res => {
						//拒绝授权
						uni.showToast({
							title: '您拒绝了请求,不能正常使用小程序',
							icon: 'none',
							duration: 2000
						});
						this.showLoading = false;
						return;
					}
				});

			}
		},
	}
</script>

<style lang="scss">
	.loginContent {
		position: absolute;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
	}

	.loginBtn {
		position: absolute;
		bottom: 40rpx;
		width: 100%;
		display: flex;
		justify-content: center;
		z-index: -1;
	}
</style>
